package org.dtrd.modules.record.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.record.entity.bean.DailyInsulinRecordBean;
import org.dtrd.modules.record.entity.bean.InsulinChart;
import org.dtrd.modules.record.entity.bean.InsulinRecordBean;
import org.dtrd.modules.record.entity.request.TimePeriodRequest;
import org.dtrd.modules.record.service.IInsulinRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_SAVE_OR_UPDATE;

/**
 * @Author: QR
 * @Date: 2021/10/17-22:53
 */
@Api(tags = "胰岛素打卡记录")
@RestController
@RequestMapping("/101/08")
public class InsulinRecordController extends BaseController {

    @Autowired
    private IInsulinRecordService insulinRecordService;

    @ApiOperation(value = "胰岛素曲线查询", response = InsulinChart.class)
    @PostMapping("/1010801")
    @AutoLog(value = "胰岛素曲线查询", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getInsulinCurve(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        String startDate = null;
        String endDate = null;
        String[] timePeriod = handleTimePeriod(request);
        if (timePeriod != null) {
            startDate = timePeriod[0];
            endDate = timePeriod[1];
        }
        if (startDate == null || endDate == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int patientId = request.getPatientId();
        InsulinChart chart = insulinRecordService.getInsulinRecordsForChart(patientId, startDate, endDate);
        return Result.OK(chart);
    }

    @ApiOperation(value = "胰岛素记录查询", response = DailyInsulinRecordBean.class)
    @PostMapping("/1010802")
    @AutoLog(value = "胰岛素记录查询", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getInsulinRecordList(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int type = getIntParam(request.getType(), 1);
        if (type == 1 || type == 3) {
            Integer page = getIntParam(request.getPage(), 1);
            Integer pageSize = getIntParam(request.getPageSize(), 20);
            String startDate = null;
            String endDate = null;
            String[] timePeriod = handleTimePeriod(request);
            if (timePeriod != null) {
                startDate = timePeriod[0];
                endDate = timePeriod[1];
            }
            if (startDate == null || endDate == null) {
                return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
            }
            Integer patientId = request.getPatientId();
            Integer count = insulinRecordService.getInsulinRecordDataCount(patientId, startDate, endDate);
            JSONObject result = new JSONObject();
            if (count != null && count > 0) {
                List<DailyInsulinRecordBean> list = insulinRecordService.getAllGroupedInsulinRecord(patientId, startDate, endDate, page, pageSize);
                result.put("recordList", list);
                result.put("totalPage", count <= pageSize || pageSize <= 0 ? 1 : count / pageSize + 1);
                result.put("count", count);
            } else {
                result.put("recordList", Collections.EMPTY_LIST);
                result.put("totalPage", 1);
                result.put("count", 0);
            }
            return Result.OK(result);
        }
        return error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }

    @ApiOperation("添加胰岛素记录")
    @PostMapping("/1010803")
    @AutoLog(value = "添加胰岛素记录", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveInsulinAdvice(@RequestBody @Validated InsulinRecordBean insulinRecordBean) {
        boolean result = insulinRecordService.saveOrUpdateInsulinRecord(insulinRecordBean);
        return result ? Result.OK() : error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }
}
